﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace 桌面视频录制
{
    [Serializable]
    public class DrawText : DrawRectangle
    {
        private string text;
        private Font font;

        public DrawText(Point leftUpPoint, string text)
        {
            this.leftUpPoint = leftUpPoint;
            this.rightUpPoint = new Point(leftUpPoint.X, leftUpPoint.Y);
            this.leftDownPoint = new Point(leftUpPoint.X, leftUpPoint.Y);
            this.rightDownPoint = new Point(leftUpPoint.X, leftUpPoint.Y);
            this.text = text;
        }
        public DrawText(Point leftUpPoint, string text, Font font)
        {
            this.leftUpPoint = leftUpPoint;
            this.rightUpPoint = new Point(leftUpPoint.X, leftUpPoint.Y);
            this.leftDownPoint = new Point(leftUpPoint.X, leftUpPoint.Y);
            this.rightDownPoint = new Point(leftUpPoint.X, leftUpPoint.Y);
            this.text = text;
            this.font = font;
        }
        public override void Draw(Graphics g)
        {
            float fontSize = 1.0f;
            StringFormat format = new StringFormat();
            if (width > height)
            {
                fontSize = width / text.Length;
            }
            else
            {
                fontSize = height / text.Length;
                format.FormatFlags = StringFormatFlags.DirectionVertical;
            }
            if (fontSize <= 0)
                return;
            Brush brush = this.DrawBrush.GetBrush();
            Font newFont = new Font(font.FontFamily, fontSize, font.Style, font.Unit);
            Matrix matrix = new Matrix();
            matrix.RotateAt((float)(angle * 180 / Math.PI), leftUpPoint);
            g.Transform = matrix;
            g.DrawString(text, newFont, brush, leftUpPoint, format);
            g.ResetTransform();
            matrix.Dispose();
            newFont.Dispose();
            format.Dispose();
        }
        public override string ToString()
        {
            string printMsg = "[文本(" + this.leftUpPoint.X + "," + this.leftUpPoint.Y + ")]->[ID:" + this.Id + "]";
            return printMsg;
        }
        public override string GetDrawObjectMsg()
        {
            string drawObjectMsg = "文本->起始位置(" + this.leftUpPoint.X + "," + this.leftUpPoint.Y + "),宽高(" + this.width + "," + this.height + "),字符数目:" + text.Length + "个,偏移角度:" + ((int)(angle * 180 / Math.PI)) + "°";
            return drawObjectMsg;
        }
    }
}
